Typical HAL I2C use cases for migration ¶
This section provides information about typical HAL I2C use cases for migration.
Use case: I2C communication in polling mode ¶
This use case shows how to initialize an I2C instance and start a master transmit operation in polling mode.
Note
The API for all communication modes was renamed between HAL1 and HAL2.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Add your init and cpnfig */
HAL_I2C_Init(&hi2c);
HAL_I2CEx_ConfigAnalogFilter(&hi2c,
I2C_ANALOGFILTER_ENABLE) ;
/* Start a I2C Process */
HAL_I2C_Master_Transmit(&hi2c,
DevAddress,
&pData,
Size,
Timeout);
|
HAL_I2C_Init(&hi2c, HAL_I2C1);
HAL_I2C_SetConfig(&hi2c, &i2c_config);
HAL_I2C_EnableAnalogFilter(&hi2c);
/* Start a I2C Process */
HAL_I2C_MASTER_Transmit(&hi2c,
device_addr,
&p_data,
size_byte,
timeout_ms);
|
Key changes ¶
HAL2 uses renamed I2C APIs (for example, HAL_I2C_MASTER_Transmit() instead of HAL_I2C_Master_Transmit()).
In HAL2, the I2C configuration is applied via HAL_I2C_SetConfig().
In HAL2, the analog filter is enabled via HAL_I2C_EnableAnalogFilter().
Use case: I2C communication in DMA mode ¶
This use case shows how to handle master and slave transmit/receive using DMA.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define I2C_ADDRESS 0x30F;
HAL_I2C_Master_Transmit_DMA(&hi2c2, 0x30F, aTxBuffer, 128);
HAL_I2C_Master_Receive_DMA(&hi2c2, 0x30F,aRxBuffer, 128);
HAL_I2C_Slave_Receive_DMA(&hi2c2, aRxBuffer, 128);
HAL_I2C_Slave_Transmit_DMA(&hi2c2, aTxBuffer, 128);
|
#define I2C_ADDRESS 0x30F;
HAL_I2C_MASTER_Transmit_DMA(&hi2cx, 0x30F, TxBuffer, NumData);
HAL_I2C_MASTER_Receive_DMA(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, NumData);
HAL_I2C_SLAVE_Receive_DMA(&hi2cx, RxBuffer, NumData);
HAL_I2C_SLAVE_Transmit_DMA(&hi2cx, TxBuffer, NumData);
|
Key changes ¶
HAL2 uses renamed I2C DMA APIs (for example, HAL_I2C_MASTER_Transmit_DMA() instead of HAL_I2C_Master_Transmit_DMA()).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_i2c_two_boards_com_dma_controller |
How to handle I2C data buffer transmission/reception between two boards in DMA mode. |
Use case: I2C master transmit/receive in interrupt mode ¶
This use case shows how to perform master transmit and master receive operations using interrupts.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
#define I2C_ADDRESS 0x30F;
HAL_I2C_Master_Transmit_IT(&hi2c2, 0x30F, aTxBuffer, TXBUFFERSIZE);
HAL_I2C_Master_Receive_IT(&hi2c2, I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE);
|
uint16_t NumData = 0;
HAL_I2C_MASTER_Transmit_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], TxBuffer, 0);
HAL_I2C_MASTER_Receive_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, 0);
|
Key changes ¶
HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_MASTER_Transmit_IT() instead of HAL_I2C_Master_Transmit_IT()).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_i2c_two_boards_com_it_controller |
How to handle I2C data buffer transmission/reception between two boards, using an interrupt. |
Use case: I2C slave transmit in interrupt mode ¶
This use case shows how to perform a slave transmit operation using interrupts.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
HAL_I2C_Slave_Transmit_IT(&hi2c2, aTxBuffer, TXBUFFERSIZE);
|
HAL_I2C_SLAVE_Transmit_IT(&hi2cx, TxBuffer, NumData);
|
Key changes ¶
HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_SLAVE_Transmit_IT() instead of HAL_I2C_Slave_Transmit_IT()).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_i2c_two_boards_com_it_controller |
How to handle I2C data buffer transmission/reception between two boards, using an interrupt. |
Use case: I2C slave receive in interrupt mode ¶
This use case shows how to perform a slave receive operation using interrupts.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
HAL_I2C_Slave_Receive_IT(&hi2c2, aRxBuffer, RXBUFFERSIZE);
|
HAL_I2C_SLAVE_Receive_IT(&hi2cx, RxBuffer, NumData);
|
Key changes ¶
HAL2 uses renamed I2C interrupt APIs (for example, HAL_I2C_SLAVE_Receive_IT() instead of HAL_I2C_Slave_Receive_IT()).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_i2c_two_boards_com_it_controller |
How to handle I2C data buffer transmission/reception between two boards, using an interrupt. |



![@startuml
participant "User Application" as App
participant "HAL I2C" as HAL_I2C
== Master Transmit DMA ==
App -> HAL_I2C: HAL_I2C_MASTER_Transmit_DMA(&hi2cx, 0x30F, TxBuffer, NumData)
== Master Receive DMA ==
App -> HAL_I2C: HAL_I2C_MASTER_Receive_DMA(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, NumData)
== Slave Receive DMA ==
App -> HAL_I2C: HAL_I2C_SLAVE_Receive_DMA(&hi2cx, RxBuffer, NumData)
== Slave Transmit DMA ==
App -> HAL_I2C: HAL_I2C_SLAVE_Transmit_DMA(&hi2cx, TxBuffer, NumData)
@enduml](../../../../../_images/plantuml-4dfaccd73f2c02bbb7c76888038202041352a1c8.png)

![@startuml
participant "User Application" as App
participant "HAL I2C" as HAL_I2C
== Master Transmit IT ==
App -> HAL_I2C: HAL_I2C_MASTER_Transmit_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], TxBuffer, 0)
== Master Receive IT ==
App -> HAL_I2C: HAL_I2C_MASTER_Receive_IT(&hi2cx, I2C_SlaveAddress[Adr_Idx], RxBuffer, 0)
@enduml](../../../../../_images/plantuml-dbc6f8c0d804ec36728dd91dfa8f631671b2cae4.png)



